package com.ruoyi.web.controller.erp;

import com.bonuo.erp.domain.EmployeeRoster;
import com.bonuo.erp.service.EmployeeRosterService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/erp/employee")
@Api(tags = "员工花名册管理")
public class EmployeeRosterController extends BaseController {
    @Autowired
    private EmployeeRosterService employeeService;

    /**
     * 查询员工花名册列表
     */
    @ApiOperation("查询员工花名册列表")
    @GetMapping("/list")
    public TableDataInfo list(EmployeeRoster employee,Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            startPage();
        }
        List<EmployeeRoster> list = employeeService.selectEmployeeRosterList(employee);
        return getDataTable(list);
    }

    /**
     * 导出员工花名册列表
     */
    @ApiOperation("导出员工花名册列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmployeeRoster employee) {
        List<EmployeeRoster> list = employeeService.selectEmployeeRosterList(employee);
        ExcelUtil<EmployeeRoster> util = new ExcelUtil<EmployeeRoster>(EmployeeRoster.class);
        util.exportExcel(response, list, "员工花名册数据");
    }

    /**
     * 新增员工
     */
    @ApiOperation("新增员工")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody EmployeeRoster employee) {
        Integer add = employeeService.insertEmployeeRoster(employee);
        return toAjax(add);
    }

    /**
     * 修改员工
     */
    @ApiOperation("修改员工")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody EmployeeRoster employee) {
        Integer update = employeeService.updateEmployeeRoster(employee);
        return toAjax(update);
    }

    /**
     * 删除员工
     * */
    @ApiOperation("删除员工")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        Integer delete = employeeService.deleteEmployeeRosterById(id);
        return toAjax(delete);
    }

    /**
     * 导入员工数据
     */
    @ApiOperation("导入员工数据")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws IOException {
        ExcelUtil<EmployeeRoster> util = new ExcelUtil<EmployeeRoster>(EmployeeRoster.class);
        List<EmployeeRoster> employeeList = util.importExcel(file.getInputStream());
        String username = "";
        try {
            username = getUsername();
        }catch (Exception e){
            username = "system";
        }
        String message = employeeService.importEmployee(employeeList, updateSupport, username);
        return success(message);
    }

    /**
     * 下载导入模板
     */
    @ApiOperation("下载导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws NoSuchFieldException, IllegalAccessException {
        ExcelUtil<EmployeeRoster> util = new ExcelUtil<EmployeeRoster>(EmployeeRoster.class);
        util.exportExcel(response, new ArrayList<>(), "员工导入模板");
    }
}